package  {
	import org.flixel.*;
	
	public class DragEye extends FlxGroup {
		public var isDragging:Boolean = false;
		public var eye:FlxSprite = new FlxSprite(820, 7840);
		public var brow:FlxSprite = new FlxSprite();
		
		public function DragEye(clickEye:ClickEye) {
			super();
			this._clickEye = clickEye;
			this.eye.loadGraphic(DragEyeImg, true, false, 75, 43);
			//this.eye.alpha = 0.65;
			
			this.brow.loadGraphic(DragEyeBrowImg, true, false, 92, 71);
			//this._eyeBrow.alpha = 0.4;
			this.brow.x = this.eye.x - 29;
			this.brow.y = this.eye.y - 27;
			this.add(this.eye);
			this.add(this.brow);
		}
		
		override public function update():void {
			if (FlxG.mouse.justPressed() )// && 
				//this.eye.overlapsPoint(
					//FlxG.mouse.screenX + FlxG.scroll.x, 
					//FlxG.mouse.screenY + FlxG.scroll.y))
			{
				this.isDragging = true;
				this.eye.frame = 1;
				FlxG.playMusic(Game.MountainMusic);
			}
			if (FlxG.mouse.justReleased() && this.isDragging) {
				this.isDragging = false;
				this.eye.frame = 0;
				if (this._clickEye.dilation > 0)
					FlxG.playMusic(Game.NormalMusic);
			}
			if (this.isDragging) {
				var eyeScreen:FlxPoint = this.eye.getScreenXY();
				var mouseVelocity:FlxPoint = new FlxPoint(
					FlxG.mouse.x - this._lastMousePosition.x,
					FlxG.mouse.y - this._lastMousePosition.y);
				eyeScreen.x += mouseVelocity.x;
				eyeScreen.y += mouseVelocity.y;
				var bleed:FlxPoint = new FlxPoint(
					this.eye.frameWidth - 7, this.eye.frameHeight - 5);
				if (eyeScreen.x > -bleed.x && 
					eyeScreen.x < (FlxG.width - this.eye.frameWidth) + bleed.x)
				{
					this.x += mouseVelocity.x;
				}
				if (eyeScreen.y > -bleed.y && 
					eyeScreen.y < (FlxG.height - this.eye.frameHeight) + bleed.y)
				{
					this.y += mouseVelocity.y;
				}
				var finalVelocity:FlxPoint = new FlxPoint(
					(this.x - this._last.x),
					(this.y - this._last.y));
				for each(var member:FlxObject in this.members) {
					member.x += finalVelocity.x;
					member.y += finalVelocity.y;
				}
			}
			this._lastMousePosition.x = FlxG.mouse.x;
			this._lastMousePosition.y = FlxG.mouse.y;
			super.update();
		}
		
		[Embed(source = '../content/DragEye.png')] private var DragEyeImg:Class;
		[Embed(source = '../content/DragEyeBrow.png')] private var DragEyeBrowImg:Class;

		private var _lastMousePosition:FlxPoint = new FlxPoint();
		private var _clickEye:ClickEye;
	}

}